﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;
using System.Collections.Generic;

public partial class admin_ymgl_ymct : System.Web.UI.Page
{
    private const string _folderUrl = "files/page_images/";
    private PageBT _pbt;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["admin_id"] == null)
            {
                Response.Redirect("admin_logout.aspx");
                return;
            }
            if (!IsPostBack)
            {
                foreach (string pt in PageTypes.types)
                {
                    this.ddlSelectPage.Items.Add(pt);
                }
                ViewState["type"] = this.ddlSelectPage.Text;
                loadTable();
            }
        }
        catch
        {

        }
    }

    private void loadTable()
    {
        string _type = ViewState["type"].ToString();
        if (string.IsNullOrEmpty(_type))
        {
            this.GridView1.DataSource = null;
            this.GridView1.DataBind();
            return;
        }
        else
        {
            _pbt = new PageBT();
            DataTable table = new DataTable();
            table.Columns.Add("name");
            table.Columns.Add("image");
            string[] pics = _pbt.GetPictures(_type);
            foreach (string pic in pics)
            {
                DataRow row = table.NewRow();
                row["name"] = pic.Trim();
                row["image"] = ResolveUrl(string.Format("{0}{1}", _folderUrl, pic.Trim()));
                table.Rows.Add(row);
            }
            this.GridView1.DataSource = table;
            this.GridView1.DataBind();
        }
    }

    protected void ddlSelectPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["type"] = this.ddlSelectPage.Text;
        loadTable();
    }

    protected void UploadPicture_Click(object sender, EventArgs e)
    {
        try
        {
            string _type = ViewState["type"].ToString();
            if (string.IsNullOrEmpty(_type))
            {
                message_show("请选择页面类型");
                return;
            }
            string LocalPath = Server.MapPath(_folderUrl); // 上传路径  
            string fe = System.IO.Path.GetExtension(this.FindPicture.FileName).ToLower();
            string fn = Guid.NewGuid().ToString() + fe;
            List<string> extList = new List<string>(new string[] { ".gif", ".png", ".jpeg", ".jpg", ".bmp" });
            if (extList.Contains(fe))
            {
                this.FindPicture.PostedFile.SaveAs(LocalPath + fn);
                PageBT pbt = new PageBT();
                if (pbt.AddPicture(_type, fn))
                {
                    message_show("上传成功，且修改数据库");
                }
                else
                {
                    message_show("上传成功，但修改数据库失败");
                }
                loadTable();
            }
            else
            {
                message_show("上传文件格式不正确，请选择图片格式");
            }
        }
        catch
        {
            message_show("上传错误 ");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string _type = ViewState["type"].ToString();
            string name = ((Button)sender).CommandArgument.ToString();
            PageBT pbt = new PageBT();
            if (pbt.DeletePicture(_type, name))
            {
                string furl = string.Format("{0}{1}", _folderUrl, name);
                FileDelete fd = new FileDelete();
                fd.deleteFile(furl);
                message_show("删除成功");
            }
            loadTable();
        }
        catch
        {
            message_show("删除失败");
        }
    }

    /// <summary>
    /// 信息提示
    /// </summary>
    /// <param name="msg">提示信息</param>
    private void message_show(string msg)
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "信息提示", "<script language='javascript'>  warning('" + msg + "');</script>");
    }
}
